package com.jingxi.search.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jingxi.common.pojo.SearchResult;
import com.jingxi.search.pojo.Item;

@Repository
public class SearchDaoImpl implements SearchDao{

	@Autowired
	private SolrServer solrServer;
	
	@Override
	public SearchResult search(SolrQuery query) throws Exception {
		//返回值对象
		SearchResult result = new SearchResult();
		//根据条件查询索引库
		QueryResponse queryResponse = solrServer.query(query);
		//取查询结果
		SolrDocumentList solrDocumentList = queryResponse.getResults();
		//取查询结果总数量
		result.setRecordCount(solrDocumentList.getNumFound());
		List<Item> items = new ArrayList<Item>();
		//取高亮显示
		Map<String,Map<String,List<String>>> highlighting = queryResponse.getHighlighting();
		//取商品列表
		for(SolrDocument solrDocument:solrDocumentList) {
			//创建一商品对象
			Item item = new Item();
			item.setId(solrDocument.get("id").toString());
			//取高亮显示的结果
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			String title = "";
			if(list != null && list.size() > 0) {
				title = list.get(0);
			}else {
				title = solrDocument.get("item_title").toString();
			}
			
			item.setTitle(title);
			item.setSell_point((String)solrDocument.get("item_sell_point"));
			item.setPrice((Long)solrDocument.get("item_price"));
			item.setImage((String)solrDocument.get("item_image"));
			item.setCategory_name((String)solrDocument.get("item_category_name"));
			item.setItem_desc((String)solrDocument.get("item_desc"));
			items.add(item);
		}
		result.setItemList(items);
		return result;
	}

}
